package vn.co.thn.android.videohot;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import vn.co.thn.android.videohot.data.VHListUploadItemData;
import vn.co.thn.android.videohot.json.VHJsonCategoryList;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Base64;
import android.util.Base64InputStream;
import android.util.Base64OutputStream;
import android.util.Log;

public class VHPreferenceManager {
	private final static String PREF_NAME = "videohot";
	private SharedPreferences mPreferences;
	private static final String PREF_DATE_START = "date_start";
	private static final String PREF_DATE_END = "date_end";
	private static final String PREF_ACCOUNT_EMAIL = "account_email";
	private static final String PREF_CATEGORY_LIST = "category_list";
	private static final String PREF_DEVICE_ID = "device_id";
	private static final String PREF_LIST_UPLOAD = "upload_list";

	public void setUploadList(VHListUploadItemData Upload_list) {
		setPreferenceObject(PREF_LIST_UPLOAD, Upload_list);
	}

	public VHListUploadItemData getUploadList() {
		return (VHListUploadItemData) getPreferenceObject(PREF_LIST_UPLOAD);
	}

	public String getDeviceID() {
		return this.getPreferenceString(PREF_DEVICE_ID);
	}

	public String getDeviceID(String defaultvalues) {
		return this.getPreferenceStringWithValuesDefault(PREF_DEVICE_ID,
				defaultvalues);
	}

	public void setDeviceID(String device_id) {
		this.setPreferenceString(PREF_DEVICE_ID, device_id);
	}

	public void setCategoryList(VHJsonCategoryList category_list) {
		setPreferenceObject(PREF_CATEGORY_LIST, category_list);
	}

	public VHJsonCategoryList getCategoryList() {
		return (VHJsonCategoryList) getPreferenceObject(PREF_CATEGORY_LIST);
	}

	public String getEmailAccount() {
		return this.getPreferenceString(PREF_ACCOUNT_EMAIL);
	}

	public String getEmailAccount(String defaultvalues) {
		return this.getPreferenceStringWithValuesDefault(PREF_ACCOUNT_EMAIL,
				defaultvalues);
	}

	public void setEmailAccount(String email_account) {
		this.setPreferenceString(PREF_ACCOUNT_EMAIL, email_account);
	}

	public void setDateStart(String Datestart) {
		this.setPreferenceString(PREF_DATE_START, Datestart);
	}

	public String getDateStart() {
		return this.getPreferenceString(PREF_DATE_START);
	}

	public String getDateStart(String DateDefault) {
		return this.getPreferenceStringWithValuesDefault(PREF_DATE_START,
				DateDefault);
	}

	public void setDateEnd(String DateEnd) {
		this.setPreferenceString(PREF_DATE_END, DateEnd);
	}

	public String getDateEnd() {
		return this.getPreferenceString(PREF_DATE_END);
	}

	public String getDateEnd(String DateDefault) {
		return this.getPreferenceStringWithValuesDefault(PREF_DATE_END,
				DateDefault);
	}

	private VHPreferenceManager(Context context) {
		mPreferences = context.getApplicationContext().getSharedPreferences(
				PREF_NAME, Context.MODE_PRIVATE);
	}

	public static VHPreferenceManager createPreferenceManager(Context context) {
		return new VHPreferenceManager(context);
	}

	private void setPreferenceString(String name, String values) {
		final Editor editor = mPreferences.edit();
		editor.putString(name, values);
		editor.commit();
	}

	private String getPreferenceString(String name) {
		return mPreferences.getString(name, null);
	}

	private String getPreferenceStringWithValuesDefault(String name,
			String valuedefault) {
		return mPreferences.getString(name, valuedefault);
	}

	/*
	 * save object
	 */
	protected void setPreferenceObject(String name, Serializable object) {
		final Editor editor = mPreferences.edit();
		editor.putString(name, objectToString(object));
		editor.commit();
	}

	protected Object getPreferenceObject(String name) {
		return (Object) stringToObject(mPreferences.getString(name, null));
	}

	/*
	 * Convert Object Serializable to String
	 */
	public static String objectToString(Serializable object) {
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		try {
			new ObjectOutputStream(out).writeObject(object);
			byte[] data = out.toByteArray();
			out.close();

			out = new ByteArrayOutputStream();
			Base64OutputStream b64 = new Base64OutputStream(out, Base64.DEFAULT);
			b64.write(data);
			b64.close();
			out.close();

			return new String(out.toByteArray());
		} catch (Exception e) {
			Log.e("save object fail",
					object.getClass().toString() + e.toString());
		}
		return null;
	}

	/*
	 * Convert String to object Serializable
	 */
	public static Object stringToObject(String encodedObject) {
		try {
			return new ObjectInputStream(new Base64InputStream(
					new ByteArrayInputStream(encodedObject.getBytes()),
					Base64.DEFAULT)).readObject();
		} catch (Exception e) {
			Log.e("get object fail", "");
		}
		return null;
	}
}
